package com.sure.supermarketserver.stock.purchase;

import com.sure.supermarketserver.common.Result;
import com.sure.supermarketserver.stock.manage.BranchName;
import com.sure.supermarketserver.stock.supplier.Supplier;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/stock/purchase")
public class PurchaseAPI {

    @Resource
    private PurchaseService purchaseService;

    //查询
    @GetMapping("")
    public Result purchaseList(PurchaseDto dto){

        Map<String, Object> page = purchaseService.getPurchaseList(dto);
        return Result.success(page);
    }


    //增加
    @PostMapping("")
    public  Result purchaseAdd(@RequestBody PurchaseDto dto){
        try {
            purchaseService.addPurchase(dto);
        } catch (Exception e) {
         return Result.fail(Result.ERR_CODE_BUSINESS, e.getMessage());
        }
        return  Result.success();
    }

    //修改
    @PutMapping("")
    public  Result purchaseUpdate(@RequestBody PurchaseDto dto){
        purchaseService.updatePurchase(dto);
        return  Result.success();
    }

    //删除
    @DeleteMapping("/{id}")
    public  Result purchaseDelete(@PathVariable Integer id){
        purchaseService.deletePurchase(id);
        return  Result.success();
    }

    //实现供应商下拉框
    @GetMapping("/supplier")
    public Result supplier(){
        List<SupplierName> supplierList = purchaseService.findSupplierList();
        return Result.success(supplierList);
    }

    //实现货物名称下拉框
    @GetMapping("/cargo")
    public Result cargo(){
        List<CargoName> cargoList = purchaseService.findCargoList();
        return  Result.success(cargoList);
    }



}
